package com.hxzy.service.Impl;

import com.hxzy.bean.Film;
import com.hxzy.dao.FilmDao;
import com.hxzy.dao.Impl.FilmDaoImpl;
import com.hxzy.service.FilmService;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.stream.Collectors;

public class FilmServiceImpl implements FilmService {
    private static FilmDao filmDao = new FilmDaoImpl();

    @Override
    public int save(Film film) {
        return filmDao.save(film);
    }

    @Override
    public int update(Film film) {
        return filmDao.update(film);
    }

    @Override
    public int remove(Integer id) {
        return filmDao.remove(id);
    }

    @Override
    public List<Film> queryAll() {
        List<Map<String, Object>> list = filmDao.queryAll();
        List<Film> collect = list.stream().map(t -> {
            int fId =  ((Long)t.get("fId")).intValue();
            Object s =t.get("fId");
            String name = s.getClass().getName();
            System.out.println(name);
            String chineseName = (String) t.get("chineseName");
            String englishName = (String) t.get("englishName");
            String category = (String) t.get("category");
            String area = (String) t.get("area");
            double duration = (double) t.get("duration");
            Timestamp release = (Timestamp) t.get("release");
            long time = release.getTime();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), TimeZone.getTimeZone("Asia/Shanghai").toZoneId());
            Film film = new Film(fId, chineseName, chineseName, category, area, duration, localDateTime);
            return film;
        }).collect(Collectors.toList());
        return collect;
    }


    @Override
    public Film findByID(Integer id) {
        return filmDao.findByID(id);
    }
}